#!/bin/bash

# 常量设置
pgsql_version="17.5" # pgsql版本
install_path="/usr/local" # 安装、操作目录
data_path="/data/pgsqldata" # 数据保存目录
pgsql_username="postgres" # 用户名
systemctl_name="pgsql" # systemctl名称

# 安装依赖
echo "......正在安装依赖......"
apt-get install -y build-essential liblz4-dev lz4 bison flex pkg-config libreadline-dev zlib1g-dev libxml2-dev libxml2 libssh-dev uuid-dev libossp-uuid-dev llvm clang libgeos-dev libproj-dev libprotobuf-c-dev osm2pgsql protobuf-c-compiler gdal-bin libgdal-dev xsltproc
if [ $? -ne 0 ]; then
  echo "依赖安装失败，脚本终止"
  exit 1
fi
echo "......依赖安装完成......"

# 下载源代码
echo "......正在下载源码包......"
wget -P ${install_path} https://mirrors.tuna.tsinghua.edu.cn/postgresql/source/v${pgsql_version}/postgresql-${pgsql_version}.tar.gz
if [ $? -ne 0 ]; then
  echo "源码包下载失败，脚本终止"
  exit 1
fi
echo "......源码包下载完成......"

# 解压缩
echo "......正在解压缩源码包......"
cd ${install_path}
tar -zxf ${install_path}/postgresql-${pgsql_version}.tar.gz
if [ $? -ne 0 ]; then
  echo "源码包解压缩失败，脚本终止"
  exit 1
fi
echo "......源码包解压缩完成......"

# 编译安装
echo "......正在编译安装......"
cd ${install_path}/postgresql-${pgsql_version}
./configure --prefix=${install_path}/pgsql --enable-debug --with-lz4 --with-openssl --with-libxml --with-ossp-uuid
if [ $? -ne 0 ]; then
  echo "配置失败，脚本终止"
  exit 1
fi
make && make install
if [ $? -ne 0 ]; then
  echo "编译安装失败，脚本终止"
  exit 1
fi
echo "......编译安装完成......"

# 添加用户组和用户
echo "......正在添加用户组和用户......"
groupadd ${pgsql_username} && useradd -g ${pgsql_username} ${pgsql_username} -m
if [ $? -ne 0 ]; then
  echo "用户组或用户添加失败，脚本终止"
  exit 1
fi
echo "......完成添加用户组和用户......"

# 配置环境变量
echo "......正在配置环境变量......"
echo "export PGHOME=/usr/local/pgsql" >> /etc/profile
echo "export PGDATA=/var/pgsqldata" >> /etc/profile
echo "PATH=\$PATH:\$PGHOME/bin:\$PGHOME/bin" >> /etc/profile
source /etc/profile # 刷新环境变量
echo "......配置环境变量完成......"

# 创建数据文件夹并移交所有权
echo "......正在创建数据文件夹......"
mkdir -p ${data_path}
chown ${pgsql_username}:${pgsql_username} ${data_path}
if [ $? -ne 0 ]; then
  echo "数据文件夹创建失败，脚本终止"
  exit 1
fi
echo "......完成创建数据文件夹......"

# 切换用户并初始化数据库
echo "......正在初始化数据库......"
su - ${pgsql_username} -c "initdb -D ${data_path}"
if [ $? -ne 0 ]; then
  echo "数据库初始化失败，脚本终止"
  exit 1
fi
echo "......完成初始化数据库......"

# 切换回root
su root

# 修改配置文件
sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/g" ${data_path}/postgresql.conf
echo "host all all 0.0.0.0/0 md5" >> ${data_path}/pg_hba.conf

# 配置systemctl脚本
echo "......正在配置systemctl脚本......"
cat>/usr/lib/systemd/system/${systemctl_name}.service<<EOF
[Unit]
Description=postgresql${pgsql_version}

[Service]
Type=forking
User=${pgsql_username}
Group=${pgsql_username}
ExecStart=/usr/local/pgsql/bin/pg_ctl -D /var/pgsqldata start
ExecReload=/usr/local/pgsql/bin/pg_ctl -D /var/pgsqldata restart
ExecStop=/usr/local/pgsql/bin/pg_ctl -D /var/pgsqldata stop

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl start ${systemctl_name}
systemctl enable ${systemctl_name}
systemctl status ${systemctl_name}
echo "......systemctl脚本配置完成......"

echo "......!!!脚本运行完成!!!......"
